<?
$pagename=LA_REG;
include("header_inc.php");
include_once 'includes/constantesMvial.php';
$lBanList = split(",", $set_banlist_users);
$lEmail =trim(getParam("email",""));
$lName = getParam("name","");
$lPassOne=getParam("passwd","");
$lPassTwo=getParam("passwd2","");
$lEmelding=getParam("emelding","");
$lValidateSign=getParam("validate_sign","");
$lHideEmail=getParam("hide_email","");
$lAgreeCond=getParam("agree_cond","");
$tplRegister=new TplLoad;
writeLinkedTitle("");
$i=0;

if (isset($_SESSION["valid_user"]))
{
	echo "<blockquote><p class='box'>".LA_ALREADY_LOGGED_DESC."</p></blockquote>";
	include("footer_inc.php");
	exit;
}

/* Extra fields handling START */
$lFieldArray=getExtraFiltered(1,"",1,$set_language); // Spesify where we are
/* Extra fields handling START */
if (is_array($lFieldArray))
{
	foreach ($lFieldArray as $lKey => $lValue)
	{ 
		if ($lValue["q_question"])
			$lFieldArray[$lKey]["q_question"]=(constant($lValue["q_question"]));
		if ($lValue["q_catdescr"])
			$lFieldArray[$lKey]["q_catdescr"]=(constant($lValue["q_catdescr"]));
		if ($lValue["q_reg_exp_errormsg"])
			$lFieldArray[$lKey]["q_reg_exp_errormsg"]=(constant($lValue["q_reg_exp_errormsg"]));
	}
}
$tplRegister->assign("extra_fields_array",$lFieldArray);

if (is_array($lFieldArray))
{
	$lReturnResultArray=ExtraFieldsInputOperations($lFieldArray);
	$lInsertArray=$lReturnResultArray[0];	// Contains data to be inserted to extra
	$errorMsg=$lReturnResultArray[1]; 		// Contains error messages
}
/* Extra fields handling FINISH */
	

if (isset($_POST["submit"]))
{
	
	$result = mysql_query ("select * from $usr_tbl where user_email = '".strToDb($lEmail) ."'");
	$lSplit=split("@",$lEmail);
	if (count($lSplit>0))
		$lDomain=$lSplit[1];
	
	$lRemoteIp=getRemoteIp();
	/*$lHostName=gethostbyaddr($lRemoteIp);
	if (!$lHostName)
		$lHostName='Not defined';*/
	

	if (mysql_num_rows($result)>0)
		$errorMsg.= "<li type='square' >".LA_ERROR_MSG20."</li>";
	
	if (!validEmail($lEmail))
		$errorMsg.= "<li type='square' >".LA_ERROR_MSG21."</li>";
	else 
	{
//		$lSql="select bl_id,bl_reason from $block_tbl where bl_email like '%".strToDb($lEmail)."%' OR bl_email='".strToDb($lDomain)."' OR bl_ip='".strToDb($lRemoteIp)."' OR bl_ip like '%".strToDb($lHostName)."%' limit 1";
		$lSql="select bl_id,bl_reason from $block_tbl where bl_email like '".strToDb($lEmail)."' limit 1";
		$lNumBannedCheck=q($lSql);
		$lRowBanned=mysql_fetch_array($lNumBannedCheck);
		
		if (mysql_num_rows($lNumBannedCheck)>0)
		{
			$errorMsg.= "<li type='square'>".LA_BLOCKED_LOGIN . ": " . $lRowBanned["bl_reason"]."</li>";
			addToHistory(5,"","","Blocked user $lEmail tried registration");
			q("update $block_tbl set bl_num_attempt=bl_num_attempt+1 where bl_id=" . $lRowBanned["bl_id"]);
		}
	}
	if ($lName=="")
		$errorMsg.= "<li type='square' >".LA_ERROR_MSG5."</li>";
	
	if (ereg(" ", $lPassOne) OR strlen($lPassOne)<$set_password_len)
		$errorMsg.= "<li type='square'>". ereg_replace("%1%",$set_password_len,LA_ERROR_MSG24)."</li>";
	
	if (($lPassOne <> $lPassTwo))
		$errorMsg.= "<li type='square'>".LA_ERROR_MSG22."</li>";
	
	if (($set_agree_conditions==1 AND !$lAgreeCond))
		$errorMsg.= "<li type='square'>".LA_CONDITIONS_MANDATORY ."</li>";
		
	if (getParam("validate_sign","")=="" && $set_img_validation)	
		$errorMsg.="<li>".LA_IMG_VER_FAILED."</li>";
	
	if (($_SESSION["validate_sign_session"] <> $_REQUEST["validate_sign"] && $set_img_validation))		
		$errorMsg.="<li>".LA_IMG_VER_FAILED."</li>";	
		
		
	if ($usr_1_mandatory AND $usr_1 == "")
		$errorMsg.= "<li type='square'>".LA_FIELD."  $usr_1_text  ". LA_MAND ."</li>";

	// Find Extra fields in user table and get values from not empty rows
	$i=0;

	
		
		
	$lRemoteIP=getRemoteIp();	
		
	if (!empty($lEmail))
		$errorMsg.=ban($lBanList, $lEmail); 
	
	if (!empty($lRemoteIP))
		$errorMsg.=ban($lBanList, getRemoteIp()); 
	
	//getRemoteIp        
             
   if (!$errorMsg)
   {
		
		$temp_validate = "";
		if ($set_opt_verify) {
			$temp_validate = generatePassword(5);
		}
		else {
			$random = 0;	
		}
		$registered = time();
		
		if (!$set_opt_verify) {
				$p = "";
		}
		else {
				$p = $lPassOne;
		}
		
		$user_id=createUser($lEmail,$lPassOne,$lName,$lEmelding,$lHideEmail,$temp_validate,$lInsertArray);
						
    if ($user_id>0)
    {
    	$tplRegister=new TplLoad;
    	$tplRegister->assign("registration_ok",1);
		
    	if ($set_payments)
    	{
    		$sql="select count(pid) as num from $pkg_tbl";
			$res_count=q($sql);
			$row_c=mysql_fetch_array($res_count);
			$num_packages=$row_c["num"];
				
			// Create new admin user if admin table is empty
			if ($num_packages<1)
			{
				q("INSERT INTO `$pkg_tbl` (`pid`, `pname`, `pterm`, `pdur`, `pactive`, `pprice`, `pcurrency`, `pupload`, `pmessages`, `pchat`, `pgb`, `protation`, `pvisitor`, `pfav`, `pmatch`, `pflirts`) VALUES (99, 'Default', 1, 3, 2, 0.00, 'USD', 3, 4, 999, 1, 1, 10, 10, 10, 10)");
				q("INSERT INTO `$pkg_tbl` (`pid`, `pname`, `pterm`, `pdur`, `pactive`, `pprice`, `pcurrency`, `pupload`, `pmessages`, `pchat`, `pgb`, `protation`, `pvisitor`, `pfav`, `pmatch`, `pflirts`) VALUES (1, 'Small', 2, 30, 1, 10.00, 'USD', 5, 20, 999, 1, 1, 1, 30, 30, 30)");
				q("INSERT INTO `$pkg_tbl` (`pid`, `pname`, `pterm`, `pdur`, `pactive`, `pprice`, `pcurrency`, `pupload`, `pmessages`, `pchat`, `pgb`, `protation`, `pvisitor`, `pfav`, `pmatch`, `pflirts`) VALUES (2, 'Medium', 1, 60, 1, 25.00, 'USD', 25, 250, 999, 1, 1, 55, 55, 55, 55)");
				q("INSERT INTO `$pkg_tbl` (`pid`, `pname`, `pterm`, `pdur`, `pactive`, `pprice`, `pcurrency`, `pupload`, `pmessages`, `pchat`, `pgb`, `protation`, `pvisitor`, `pfav`, `pmatch`, `pflirts`) VALUES (3, 'Gold', 1, 90, 1, 60.00, 'USD', 200, 200, 999, 1, 1, 200, 200, 200, 200)");
				
			}
    		addPackage($user_id,99);
    	}
    	if ($set_opt_verify)
		{
			$lNewUserVerifyArray=array("$lName","$lEmail","$lPassOne","$set_sitename","".getRemoteIp()."","http://$set_url/member_login.php","http://$set_url/verify.php?verify=$temp_validate");
			$lSubject = formatString(LA_MAIL_VAL_SUBJECT, $lNewUserVerifyArray);
			$lBody = formatString(LA_MAIL_VAL_BODY, $lNewUserVerifyArray);
						
			sendEmail($lEmail, $set_webmaster_address, $lSubject, $lBody);
			$tplRegister->assign("verify",1);
		}
		elseif ($set_approve_mem)
		{
			//$sql = "update $usr_tbl set months='" .round($_REQUEST["months"]) ."' where user_id=$user_id";
			//$r = q($sql);
			$tplRegister->assign("wait_approval",1);
		}
		else
		{
			$lNewUserArray=array("$lName","$lEmail","$lPassOne","$set_sitename","".getRemoteIp()."","http://$set_url/member_login.php");
			$lSubject = formatString(LA_MAIL_NEWUSER_SUBJECT, $lNewUserArray);
			$lBody = formatString(LA_MAIL_NEWUSER_BODY, $lNewUserArray);
			//JMR: Agregue este iF para podes desactivar de manera temporal el email de bienvenida
			if(SEND_WELCOME_EMAIL!=0){				
				sendEmail($lEmail,$set_webmaster_address, $lSubject, $lBody);
			}			
			$_SESSION["valid_user"] = $user_id;
        	$_SESSION["user_name"] = $lName;
			$_SESSION["user_email"] = $lEmail;
        	addLoginItem($user_id);
			addToHistory(1,$user_id,"","");
			
			// Redirect user to correct page
			redirect("member.php");
			
			
		}
 	}
 } 
}


if (!isset($_POST["submit"]) OR (isset($_POST["submit"]) AND !empty($errorMsg)))
{
	
	$tplRegister->assign("show_regform",1);
	
	$tplRegister->caching = false;
	$tplRegister->assign("name",strEnc(getParam("name","")));
	$tplRegister->assign("email",strEnc(getParam("email","")));
	$tplRegister->assign("passwd",strEnc(getParam("passwd","")));
	$tplRegister->assign("passwd2",strEnc(getParam("passwd2","")));
	$tplRegister->assign("emelding",strEnc(getParam("emelding","")));
	$tplRegister->assign("hide_email",getParam("hide_email",""));
	$tplRegister->assign("agree_cond",getParam("agree_cond",""));
	$tplRegister->assign("la_pass",eregi_replace("%1%",$set_password_len,LA_PASS1));
	$tplRegister->assign("set_agree_conditions",$set_agree_conditions);
	$tplRegister->assign("LA_CONDITIONS_TEXT",formatString(LA_CONDITIONS,array("<label onclick=\"javascript:showPopUpTerminosCondiciones();\" class=\"fakeLinks2\" style=\"color: orange;\">","</label>")));
	$tplRegister->assign("errorMsg",$errorMsg);
	
	if ($set_img_validation==1)
		$tplRegister->assign("image_validation",1);
		
	

}
$tplRegister->display("register.tpl");
include_once("footer_inc.php");
?>
